package com.apchernykh.rozhlasapp.utils;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLParsingHelper {
	public static String extractChildText(Node iNode, String iElementName) {
		Element aFstElmnt = (Element) iNode;
		NodeList aNodeList = aFstElmnt.getElementsByTagName(iElementName);
		if (aNodeList != null) {
			Element aSecondElmnt = (Element) aNodeList.item(0);
			if (aSecondElmnt == null) {
				return "";
			}
			NodeList aSecondNodeList = aSecondElmnt.getChildNodes();
			if (aSecondNodeList != null && aSecondNodeList.item(0) != null) {
				return aSecondNodeList.item(0).getNodeValue();
			}
		}
		return "";
	}

	static public class TimedNamedEvent {
		public int mFrom;
		public int mUntil;
		public String mName;
	}

	public static TimedNamedEvent extractTimedNamedEvent(Node iNode,
			String iElementName) {
		TimedNamedEvent aRetVal = new TimedNamedEvent();
		Element aFirstElmnt = (Element) iNode;
		NodeList aNodeList = aFirstElmnt.getElementsByTagName(iElementName);
		if (aNodeList != null) {
			Element aSecondElmnt = (Element) aNodeList.item(0);
			if (aSecondElmnt != null) {
				NodeList aSecondNodeList = aSecondElmnt.getChildNodes();
				if (aSecondNodeList != null && aSecondNodeList.item(0) != null) {
					aRetVal.mName = aSecondNodeList.item(0).getNodeValue();
				}
				Attr aFrom = aSecondElmnt.getAttributeNode("od");
				Attr aUntil = aSecondElmnt.getAttributeNode("do");
				if(aFrom != null && aUntil != null)
				{
					aRetVal.mFrom = Integer.decode(aFrom.getValue());
					aRetVal.mUntil = Integer.decode(aUntil.getValue());
				}
			}
		}
		return aRetVal;
	}

}
